/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.approval;

import org.openaecfm.model.approval.Approval;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class ApprovalIO extends AbstractEntityInstanceInputImpl<Approval>
		implements IEntityInstanceOutput<Approval> {

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(Approval pojo, IAppDataManager appDataManager) {

		SimpleEntityInstance ei = new SimpleEntityInstance("IFCAPPROVAL",
				appDataManager.getName(pojo));

		// Set Identifier
		if (pojo.getIdentifier() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getIdentifier(),
					StringParameter.UTF_8));
		}

		// Set Name
		if (pojo.getName() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getName(),
					StringParameter.UTF_8));
		}

		// Set Description
		if (pojo.getDescription() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getDescription(),
					StringParameter.UTF_8));
		}

		// Set TimeOfApproval
		if (pojo.getTimeOfApproval() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getTimeOfApproval(),
					StringParameter.UTF_8));
		}

		// Set Status
		if (pojo.getStatus() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getStatus(),
					StringParameter.UTF_8));
		}

		// Set Level
		if (pojo.getLevel() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getLevel(),
					StringParameter.UTF_8));
		}

		// Set Qualifier
		if (pojo.getQualifier() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getQualifier(),
					StringParameter.UTF_8));
		}

		// Set RequestingApproval
		if (pojo.getRequestingApproval() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getRequestingApproval())));
		}

		// Set GivingApproval
		if (pojo.getGivingApproval() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getGivingApproval())));
		}

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected Approval doCreateObject() {
		return new Approval();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei, Approval pojo,
			IAppDataManager appDataManager) {

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set RequestingApproval
		Parameter requestingApproval = sei.getParameter(7);
		if (requestingApproval.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter e = (EntityInstanceNameParameter) requestingApproval;
			pojo.setRequestingApproval(appDataManager.getObject(e
					.toInstanceName()));
		}

		// Set GivingApproval
		Parameter givingApproval = sei.getParameter(8);
		if (givingApproval.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter e = (EntityInstanceNameParameter) givingApproval;
			pojo.setGivingApproval(appDataManager.getObject(e.toInstanceName()));
		}
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei, Approval pojo) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Identifier
		Parameter identifier = sei.getParameter(0);
		if (identifier.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) identifier;
			pojo.setIdentifier(s.toUTF8());
		}

		// Set Name
		Parameter name = sei.getParameter(1);
		if (name.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) name;
			pojo.setName(s.toUTF8());
		}

		// Set Description
		Parameter description = sei.getParameter(2);
		if (description.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) description;
			pojo.setDescription(s.toUTF8());
		}

		// Set TimeOfApproval
		Parameter timeOfApproval = sei.getParameter(3);
		if (timeOfApproval.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) timeOfApproval;
			pojo.setTimeOfApproval(s.toUTF8());
		}

		// Set Status
		Parameter status = sei.getParameter(4);
		if (status.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) status;
			pojo.setStatus(s.toUTF8());
		}

		// Set Level
		Parameter level = sei.getParameter(5);
		if (level.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) level;
			pojo.setLevel(s.toUTF8());
		}

		// Set Qualifier
		Parameter qualifier = sei.getParameter(6);
		if (qualifier.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) qualifier;
			pojo.setQualifier(s.toUTF8());
		}
	}
}
